package com.softqin.washer.utils;

import com.softqin.washer.common.WasherApp;

import android.util.Log;


/**
 *creator    ：      ruan
 *create date：     2015年1月15日
 *description：      日志打印类，所有的日志在这里面打印     
 */
public class LogUtils {
    private static boolean printLog = true;
    public static boolean isWriteLog = true;

    public static void d(final String tag, String message) {
        if (printLog) {
            Log.d(tag, message);
        }

        if (isWriteLog) {
        	FileUtils.writeLog(WasherApp.getAppContext(), tag, message);
        }
    }

    public static void v(final String tag, String message) {
        if (printLog) {
            Log.v(tag, message);
        }

        if (isWriteLog) {
        	FileUtils.writeLog(WasherApp.getAppContext(), tag, message);
        }
    }

    public static void i(final String tag, String message) {
        if (printLog) {
            Log.i(tag, message);
        }

        if (isWriteLog) {
        	FileUtils.writeLog(WasherApp.getAppContext(), tag, message);
        }
    }

    public static void w(final String tag, String message) {
        if (printLog) {
            Log.w(tag, message);
        }

        if (isWriteLog) {
        	FileUtils.writeLog(WasherApp.getAppContext(), tag, message);
        }
    }

    public static void e(final String tag, String message) {
        if (printLog) {
            Log.e(tag, message);
        }

        if (isWriteLog) {
        	FileUtils.writeLog(WasherApp.getAppContext(), tag, message);
        }
    }

    public static void e(final String tag, Exception ex) {
        String message = getException(ex);

        if (null != message && !message.isEmpty()) {
            Log.e(tag, message);

//            Utility.writeLog(IMApplication.getAppContext(), tag, message);
        }

    }

    public static String getException(Exception ex) {
        StringBuffer exceptionStr = new StringBuffer();
        exceptionStr.append("\n" + "uncaughtException getLocalizedMessage--: " + ex.getLocalizedMessage() + "\n");
        StackTraceElement[] elements = ex.getStackTrace();
        for (int i = 0; i < elements.length; i++) {
            exceptionStr.append(elements[i].toString() + "\n");
        }
        return null;
    }

    public static String getException(Throwable ex) {
        StringBuffer exceptionStr = new StringBuffer();
        exceptionStr.append("\n" + "uncaughtException getLocalizedMessage--: " + ex.getLocalizedMessage() + "\n");
        StackTraceElement[] elements = ex.getStackTrace();
        for (int i = 0; i < elements.length; i++) {
            exceptionStr.append(elements[i].toString() + "\n");
        }
        return null;
    }

}
